package marsrovers;

public class RoverPosition {
	
	private Point point;
	private Orientation orientation;
	
	public RoverPosition(int x, int y, String dirCode) {
		point = new Point(x, y);
		this.orientation = Orientation.toOrientation(dirCode);
	}
	
	public Orientation getorientation() {
		return orientation;
	}
	
	public void setorientation(Orientation orientation) {
		this.orientation = orientation;
	}

	public Point getPoint() {
		return point;
	}

	public void setPoint(Point point) {
		this.point = point;
	}
	
	public void update(Instruction i) {
		if (isMoveLeftOrRight(i)) {
			orientation = OrientationHelper.getNewOrientation(orientation, i);
		} else {
			point.advance(orientation);
		}
	}

	private boolean isMoveLeftOrRight(Instruction i) {
		return (i == Instruction.L) || (i == Instruction.R);
	}
	
	public String toString() {
		return point.toString() + " " + orientation.toString();
	}
	
	public static RoverPosition toRoverPosition(String line) {
		String[] strings = line.split(" ");
		
		String pointX = strings[0];
		String pointY = strings[1];
		String orientation = strings[2];
		
		return new RoverPosition(
				Integer.valueOf(pointX), 
				Integer.valueOf(pointY), 
				orientation);
	}

}
